package com.tongji.housediy.view
{
	import caurina.transitions.Tweener;
	
	import com.tongji.housediy.events.commonEvent.GridContainerEvent;
	import com.tongji.housediy.events.commonEvent.SetCompStateEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideControlPanelEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideFilterEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.SelectedObjectModel;
	import com.tongji.housediy.view.components.AbstractBorderChild;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.Furniture;
	import com.tongji.housediy.view.components.GridContainer;
	import com.tongji.housediy.view.components.IndependentFloor;
	import com.tongji.housediy.view.components.InteriorWall;
	import com.tongji.housediy.view.components.OpeningWall;
	import com.tongji.housediy.view.components.RoomBorder;
	import com.tongji.housediy.view.components.RoomFloor;
	import com.tongji.housediy.view.titlewindows.GoodsTitleWindow;
	import com.tongji.housediy.view.titlewindows.OpenWallTitleWindow;
	import com.tongji.housediy.view.titlewindows.RoomTitleWindow;
	import com.tongji.housediy.view.titlewindows.WallTitleWindow;
	
	import de.polygonal.ds.NullIterator;
	
	import flash.display.CapsStyle;
	import flash.display.GraphicsPath;
	import flash.display.GraphicsPathCommand;
	import flash.display.GraphicsSolidFill;
	import flash.display.GraphicsStroke;
	import flash.display.IGraphicsData;
	import flash.display.JointStyle;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.FlexGlobals;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.events.FlexEvent;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	import mx.managers.PopUpManagerChildList;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.TitleWindow;
	import spark.filters.GlowFilter;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class DrawingBoardMediator extends Mediator
	{
		[Inject]
		public var view:DrawingBoard;
		
		[Inject]
		public var selectedModel:SelectedObjectModel;
		
		[Inject]
		public var exchangeDataModel:ExchangeDataModel;
		
		private var titleWindow:TitleWindow;
		private var host:UIComponent = null;
		
		override public function onRegister():void
		{	
			view.addGridAndMask();
			
			exchangeDataModel.gridContainer = view.gridContainer;
			
			view.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyBoardDownHandler);
			view.stage.addEventListener(KeyboardEvent.KEY_UP, keyBoardUpHandler);
			
			view.stage.addEventListener(Event.RESIZE, stageResizeHandler);
			
			this.addContextListener(ShowHideControlPanelEvent.SHOW_CONTROL_PANEL, showControlPanelHandler, ShowHideControlPanelEvent);
			this.addContextListener(ShowHideControlPanelEvent.HIDE_CONTROL_PANEL, hideControlPanelHandler, ShowHideControlPanelEvent);
			
			this.addContextListener(GridContainerEvent.RETRIEVE_GRID_CONTAINER, retrieveGridContainerHandler, GridContainerEvent);
						
			//监听弹出框事件
			view.addEventListener(ShowHideTitleWindowEvent.SHOW_TITLE_WINDOW, showTitleWindowHandler);
			view.addEventListener(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW, hideTitleWindowHandler);
			
			//监听高亮事件
			view.addEventListener(ShowHideFilterEvent.SHOW_FILTER, showFilterHandler);
			view.addEventListener(ShowHideFilterEvent.HIDE_FILTER, hideFilterHandler);
			
			view.addEventListener(ShowHideTitleWindowEvent.UPDATE_TITLE_WINDOW_POSITION, updateTitleWindowPositionHandler);
			this.addContextListener(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW, hideTitleWindowHandler, ShowHideTitleWindowEvent);
			
			this.addContextListener(SetCompStateEvent.RESET_FILTER, setCompFilterHandler, SetCompStateEvent);
			//view.addEventListener(SetCompStateEvent.RESET_FILTER, setCompFilterHandler);
		}
		
		private function showFilterHandler(event:ShowHideFilterEvent):void
		{	
				event.host.filters= [new GlowFilter(0xffff00,1,8,8,3,1,false,false)];
				host = event.host;
		}
		
		private function hideFilterHandler(event:ShowHideFilterEvent):void
		{		
			if(host)
				host.filters = null;			   
		}
		
		private function setCompFilterHandler(event:SetCompStateEvent):void
		{
			dispatch(new SetCompStateEvent(SetCompStateEvent.RESET_FILTER));			
		}
		
		private function keyBoardDownHandler(event:KeyboardEvent):void
		{
			if (event.keyCode == 88)
				view.gridContainer.isXKeyDown = true;
			else if (event.keyCode == 89)
				view.gridContainer.isYKeyDown = true;
		}
		
		private function keyBoardUpHandler(event:KeyboardEvent):void
		{
			view.gridContainer.isXKeyDown = false;
			view.gridContainer.isYKeyDown = false;
		}
		
		private function stageResizeHandler(event:Event):void
		{
			view.updateGridAndMask();
		}
		
		private function showControlPanelHandler(event:ShowHideControlPanelEvent):void
		{
			view.width = view.stage.stageWidth - 272;
			Tweener.addTween(view.gridContainer, {x: view.width / 2 - 4000, time: 0.5, onComplete: view.updateGridAndMask});
		}
		
		private function hideControlPanelHandler(event:ShowHideControlPanelEvent):void
		{
			view.width = view.stage.stageWidth - 10;
			view.updateGridAndMask();
			Tweener.addTween(view.gridContainer, {x: view.width / 2 - 4000, time: 0.5});
		}		
		
		private function retrieveGridContainerHandler(event:GridContainerEvent):void
		{
			Tweener.addTween(view.gridContainer, {x: view.width / 2 - 4000, y: view.height / 2 - 3000, scaleX: 1, scaleY: 1, time: 0.5});
		}
		
		private function showTitleWindowHandler(event:ShowHideTitleWindowEvent):void
		{
			if (!titleWindow)
			{
				selectedModel.selectedObj = event.host;
				
				if (event.host is RoomBorder || (event.host is RoomFloor && event.host.parent is IndependentFloor))
					titleWindow = new WallTitleWindow();
				else if (event.host is RoomFloor)
					titleWindow = new RoomTitleWindow();
				else if (event.host is OpeningWall)
					titleWindow = new OpenWallTitleWindow();
				else
					titleWindow = new GoodsTitleWindow();
				
				PopUpManager.addPopUp(titleWindow, view);
				mediatorMap.createMediator(titleWindow);
			}
			titleWindow.x = (int)(event.windowX - titleWindow.width / 2);
			titleWindow.y = (int)(event.windowY - titleWindow.height / 2);
		}
		
		private function hideTitleWindowHandler(event:ShowHideTitleWindowEvent):void
		{
			PopUpManager.removePopUp(titleWindow);
			mediatorMap.removeMediatorByView(titleWindow);
			titleWindow = null;
			
			selectedModel.selectedObj = null;
		}
		
		private function updateTitleWindowPositionHandler(event:ShowHideTitleWindowEvent):void
		{
			titleWindow.x += event.firstX;
			titleWindow.y += event.firstY;
		}
	}
}